import 'package:flutter/material.dart';
import 'router/router.dart';
import 'unknown.dart';
import 'about.dart';
import 'detail.dart';

main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primaryColor: Colors.teal, cardColor: Colors.orange),
      debugShowCheckedModeBanner: false,
      routes: HYRouter.routes,
      initialRoute: HYRouter.initialRoute,
      onGenerateRoute: HYRouter.generateRoute,
      onUnknownRoute: HYRouter.unknownRoute,
      // home: HYHomePage(),
    );
  }
}

class HYHomePage extends StatefulWidget {
  static const String routeName = '/';

  @override
  _HYHomePageState createState() => _HYHomePageState();
}

class _HYHomePageState extends State<HYHomePage> {
  String _homeMessage = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Demo'),
      ),
      body: Center(
          child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(_homeMessage,
              style: TextStyle(fontSize: 20, color: Colors.teal)),
          RaisedButton(
              child: Text('跳转到详情'), onPressed: () => _jumpToDetail(context)),
          RaisedButton(
              child: Text('跳转到关于'), onPressed: () => _jumpToAbout(context)),
          RaisedButton(
              child: Text('跳转到详情'), onPressed: () => _jumpToDetail2(context)),
          RaisedButton(
              child: Text('跳转到设置'), onPressed: () => _jumpToSettings(context))
        ],
      )),
      backgroundColor: Colors.white,
      floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add),
          backgroundColor: Colors.teal,
          onPressed: () {
            print('FloatingActionButton click');
          }),
      floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
    );
  }

  void _jumpToDetail(BuildContext context) {
    // Navigator.push(context, route)
    // 1.普通跳转方式
    Future result =
        Navigator.of(context).push(MaterialPageRoute(builder: (ctx) {
      return HYDetailPage('a home data');
    }));
    result.then((value) {
      setState(() {
        _homeMessage = value;
      });
    });
  }

  void _jumpToAbout(BuildContext context) {
    Navigator.of(context)
        .pushNamed(HYAboutPage.routeName, arguments: 'a home message')
        .then((value) {
      setState(() {
        _homeMessage = value;
      });
    });
  }

  void _jumpToDetail2(BuildContext context) {
    Navigator.of(context)
        .pushNamed(HYDetailPage.routeName, arguments: 'a home details2')
        .then((value) {
      setState(() {
        _homeMessage = value;
      });
    });
  }

  _jumpToSettings(BuildContext context) {
    Navigator.of(context).pushNamed('/settings');
  }
}
